#!/usr/bin/python3
# @Time    : 2018/6/1 下午 19:42:08
# @Author  : LaoJI

from . import web
import json
from flask import render_template, jsonify, request
from app.models.user import User
from flask_login import current_user


@web.route('/user/checkusername', methods=['POST'])
def checkusername():
    res = {}
    form = request.form
    print(form)
    uname = form["username"]
    print(uname)
    if User.query.filter_by(userid=uname).first():
        res["isExist"] = True
    else:
        res["isExist"] = False
    return jsonify(res)


@web.route('/islogin', methods=['Get'])
def islogin():
    res = {}
    if not current_user.is_anonymous:
        uid = current_user.get_id()
        user = User.query.get(int(uid))
        res["data"] = {
            "nickname": user.nickname,
            "id": user.id,
            "avatar_url":user.avatar_url
        }
        # return json.dumps(res, default=lambda o: o.__dict__)
    else:
        res["data"] = False
    return jsonify(res)
